package com.example.xjtuse_pc.mobilesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.Toast;

import com.example.xjtuse_pc.mobilesafe.Atools.NumberAddressActivity;
import com.example.xjtuse_pc.utils.SmsUtils;

public class AtoolsActivity extends Activity {

    private ProgressDialog pd;
    SmsUtils sm=new SmsUtils();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atools);


    }


    public void numberQuery(View view) {
        Intent intent=new Intent(AtoolsActivity.this, NumberAddressActivity.class);
        startActivity(intent);
    }

    public void saveSms(View view) throws Exception {
        pd=new ProgressDialog(AtoolsActivity.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);


        pd.setTitle("短信备份进度");
        pd.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    sm.saveSms(AtoolsActivity.this, new SmsUtils.SmsBcakUp() {
                        @Override
                        public void beforeBackup(int max) {
                            pd.setMax(max);
                        }

                        @Override
                        public void onSmsSetUp(int progress) {
                            pd.setProgress(progress);
                        }
                    });
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AtoolsActivity.this, "手机短信已经备份", Toast.LENGTH_SHORT).show();
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AtoolsActivity.this, "手机短信备份失败", Toast.LENGTH_SHORT).show();
                        }
                    });

                }finally {
                    pd.dismiss();
                }

            }
        }).start();




    }

    public void getSms(View view) throws Exception {

         AlertDialog.Builder ab=new AlertDialog.Builder(this);
        ab.setTitle("警告:还原短信会先将手机原有短信删除!!!");

        ab.setNegativeButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        pd=new ProgressDialog(AtoolsActivity.this);
                        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        pd.setTitle("短信还原进度");

                        pd.show();
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            sm.getSms(AtoolsActivity.this, new SmsUtils.SmsBcakUp() {
                                                @Override
                                                public void beforeBackup(int max) {
                                                    pd.setMax(max);
                                                }
                                                @Override
                                                public void onSmsSetUp(int progress) {
                                                    pd.setProgress(progress);
                                                }
                                            });

                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(AtoolsActivity.this, "手机短信还原成功", Toast.LENGTH_SHORT).show();
                                                }
                                            });

                                        } catch (Exception e) {
                                            e.printStackTrace();
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(AtoolsActivity.this, "手机短信还原失败", Toast.LENGTH_SHORT).show();
                                                }
                                            });
                                        } finally {
                                            pd.dismiss();
                                        }
                                    }
                                }).start();


                            }





                }

        );
        ab.setPositiveButton("取消", new DialogInterface.OnClickListener()

                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();


                    }
                }

        );
        ab.show();



        }
    }
